<?php

class Signup extends MY_Controller {

	public function __construct() {
		parent::MY_Controller();

		$this->load->helper(array('form','url', 'language'));
		$this->load->library('form_validation');

		$this->lang->load('welcome');
		$this->lang->load('login');
		$this->lang->load('profile');
		$this->lang->load('signup');
	}

	public function index() {
		try {
			if ($this->input->post('action')) {
				if ($this->_submit_validate() === TRUE) {
					$this->_register();
				}
				else {
					$this->render('signup/index', null, $this->error(validation_errors('<li>','</li>')));
				}
			}
			else {
				$this->render('signup/index');
			}
		}
		catch (Exception $e) {
			echo '<pre>'; echo $e; echo '</pre>';
		}
	}

	private function _register() {
		$p = new PersonModel();
		$p->merge($_POST);
		$p->Pilot = new PilotModel();

		try {
			$p->save();

			// find the 'User' role and add it to the new user
			$p->Roles[] = RoleModel::findByName('User');
			$p->save();

			/*if ($this->input->post('cnd_user')) {
				$this->_migrateAccount($p);
			}*/
			
			$this->redirect('login', 'Account created successfully!');
		}
		catch (Exception $e) {
			$this->render('signup/index', null, $this->error(array(
                        'message' => 'Ooops, the account could not be created - please try again later.',
                        'exception' => $e
			)));
		}
	}
	
	private function _migrateAccount(&$person) {
		$phpbbUser = Doctrine::getTable('PhpbbUserModel')->findOneByUsername($this->input->post('cnd_user'));
		
		if ($phpbbUser == null) {
			throw new Exception("Could not find user in PHPBB tables!");
		}
		
		$leonardoUser = Doctrine::getTable('LeonardoPilotModel')->findOneByPilotID($phpbbUser->user_id);
		
		if ($leonardoUser == null) {
			throw new Exception("Could not find user in Leonardo tables!");
		}
		
		// pilot
		$pilot = $person->Pilot;
        $pilot->cnd_user = $this->input->post('cnd_user');
		
		$pilot->helmet = $leonardoUser->helmet;
        $pilot->harness = $leonardoUser->harness;
        $pilot->gps = $leonardoUser->gps;
        $pilot->vario = $leonardoUser->vario;
        $pilot->rescue = $leonardoUser->reserve;
        $pilot->sponsor = $leonardoUser->sponsor;
        $pilot->comments = $leonardoUser->personalwebpage;
		
        $pilot->save();
	}

	private function _submit_validate() {

		// validation rules
		$this->form_validation->set_rules('id', 'Civl ID',
			'required|numeric|min_length[3]|max_length[12]|unique[PersonModel.id]');

		$this->form_validation->set_rules('password', 'Password',
			'required|min_length[3]|max_length[12]');

		$this->form_validation->set_rules('passconf', 'Confirm Password',
			'required|matches[password]');

		$this->form_validation->set_rules('name', 'Name',
			'required|xss_clean');

		$this->form_validation->set_rules('email_primary', 'E-mail',
			'required|valid_email|unique[PersonModel.email_primary]');

		return $this->form_validation->run();

	}

}
